package cn.zl.ymcc.controller;

import cn.zl.ymcc.dto.CourseSaveDto;
import cn.zl.ymcc.entity.Course;
import cn.zl.ymcc.query.CourseQuery;
import cn.zl.ymcc.result.PageResult;
import cn.zl.ymcc.result.Result;
import cn.zl.ymcc.service.CourseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zl
 * @since 2025-10-21
 */
@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    private CourseService courseService;

    @Operation(summary = "创建")
    @PostMapping("/save")
    public Result<Boolean> save(@RequestBody CourseSaveDto courseSaveDto) {
        courseService.insert(courseSaveDto);
        return Result.success();
    }

    @Operation(summary = "更新")
    @PutMapping
    public Result<Boolean> update(@RequestBody Course course) {
        boolean result = courseService.updateById(course);
        return result ? Result.success() : Result.error("更新失败");
    }

    @Operation(summary = "删除")
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(
            @Parameter(description = "ID", required = true)
            @PathVariable Long id) {
        boolean result = courseService.removeById(id);
        return result ? Result.success() : Result.error("删除失败");
    }

    @Operation(summary = "根据ID查询")
    @GetMapping("/{id}")
    public Result<Course> findById(
            @Parameter(description = "ID", required = true)
            @PathVariable Long id) {
        Course course = courseService.getById(id);
        return course != null ? Result.success(course) : Result.error("数据不存在");
    }

    @Operation(summary = "查询所有")
    @GetMapping("/list")
    public Result<List<Course>> findAll() {
        List<Course> list = courseService.list();
        return Result.success(list);
    }

    /*@Operation(summary = "分页查询")
    @GetMapping("/pagelist")
    public Result<PageResult<Course>> findPage(
            @Parameter(description = "页码", example = "1")
            @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页数量", example = "10")
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<Course> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        IPage<Course> pageResult = courseService.page(page, queryWrapper);
        PageResult<Course> result = PageResult.build(
                pageResult.getTotal(),
                pageResult.getRecords(),
                pageNum,
                pageSize
        );
        return Result.success(result);
    }*/
    @Operation(summary = "分页查询")
    @PostMapping("/pagelist")
    public Result<PageResult<Course>> findPage(@RequestBody CourseQuery query
    ) {
        Page<Course> page = new Page<>(query.getPage(), query.getRows());
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();

        IPage<Course> pageResult = courseService.page(page, queryWrapper);
        PageResult<Course> result = PageResult.build(
                pageResult.getTotal(),
                pageResult.getRecords(),
                query.getPage(),
                query.getRows()
        );
        System.err.println(query);
        return Result.success(result);
    }
    @Operation(summary = "课程上线")
    @PostMapping("/onLineCourse")
    public Result onLineCourse(@RequestBody List< Long> ids) {
        courseService.onLineCourse(ids);
       return Result.success();
    }

}